package com.pig4cloud.pig.homecenter.ws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.plugin.websocket.config.WebSocketMessageSender;

import java.util.Map;

public class HassWebsocketMessageUtils {

	public final static String CONFIG_ENTITY_REGISTRY_GET = "config/entity_registry/get";
	public final static String CALL_SERVICE = "call_service";

	public static void configEntityRegistryGet(String entityId) {
		JSONObject json = new JSONObject();
		json.put("type", CONFIG_ENTITY_REGISTRY_GET);
		json.put("entity_id", entityId);

		sendHassMessage(json.toJSONString());
	}


	public static void callService(String domain, String service, Map serviceData) {
		JSONObject json = new JSONObject();
		json.put("type", CALL_SERVICE);
		json.put("return_response", false);
		json.put("service", service);
		json.put("domain", domain);
		json.put("service_data", serviceData);
		sendHassMessage(json.toJSONString());

	}

	public static void sendHassMessage(String message) {
		PigUser user = SecurityUtils.getUser();
		WebSocketMessageSender.send("hass_" + user.getUsername(),message);
	}

	public static void sendWebMessage(String message) {
		PigUser user = SecurityUtils.getUser();
		WebSocketMessageSender.send("pig_" + user.getUsername(),message);
	}
}
